package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.Constants;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.entity.*;
import com.yuyou.fn.platform.service.*;
import com.yuyou.fn.platform.vo.SysResourceVo;
import com.yuyou.fn.platform.vo.SysUserQueryVo;
import com.yuyou.fn.platform.vo.SysUserSaveOrUpdateVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin")
public class AdminSysApiController extends AdminApiBaseController {

    @Resource
    private ISysUserService sysUserService ;

    @Resource
    private ISysResourceService sysResourceService ;

    @Resource
    private ISysRoleResourceService sysRoleResourceService ;

    @Resource
    private ISysRoleService sysRoleService ;

    @Resource
    private ISysUserRoleService sysUserRoleService ;

    @Resource
    private IBusTeamService busTeamService ;

    @Resource
    private IAreaTeamService areaTeamService ;

    @Resource
    private IPeriodService periodService ;



    @ApiOperation("系统用户列表")
    @RequestMapping(value = "/sysUserList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<SysUser>> sysUserList(SysUserQueryVo sysUserQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        SysUser user = getUser(request);
        if(user.getUserType() == Constants.USER_TYPE_OF_AREA ){
            sysUserQueryVo.setAreaTeamId(getUser(request).getAreaTeamId());
        }else if(user.getUserType() == Constants.USER_TYPE_OF_BUS ){
            sysUserQueryVo.setBusTeamId(user.getBusTeamId());
        }
        Page<SysUser> page = sysUserService.findSysUserByQueryForPage(sysUserQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("系统用户详情")
    @RequestMapping(value = "/sysUserDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<SysUser> sysUserDetail(@RequestParam("userId") Long userId) {
        SysUser sysUser = sysUserService.findById(userId);
        return ReturnResult.success(sysUser);
    }

    @ApiOperation("删除系统用户")
    @RequestMapping(value = "/delSysUser" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delSysUser(@RequestParam("userId") Long userId) {
        sysUserService.deleteUser(userId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("保存更新系统用户")
    @RequestMapping(value = "/saveOrUpdateSysUser" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateSysUser(SysUserSaveOrUpdateVo sysUserSaveOrUpdateVo, HttpServletRequest request ) {
        //sysUserSaveOrUpdateVo.setAreaTeamId(getUser(request).getAreaTeamId());
        sysUserService.saveOrUpdateSysUser(sysUserSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("更新用户状态系统用户")
    @RequestMapping(value = "/updateSysUserStatus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> updateSysUserStatus(@RequestParam("userId") Long userId ,@RequestParam("status") Integer status ) {
        sysUserService.updateSysUserStatus(userId,status);
        return ReturnResult.success("Ok");
    }



    @ApiOperation("系统资源列表")
    @RequestMapping(value = "/sysResourceList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<SysResourceVo>> sysResourceList() {
        List<SysResourceVo> allResource = sysResourceService.findAllResource();
        return ReturnResult.success(allResource);
    }

    @ApiOperation("角色资源列表")
    @RequestMapping(value = "/roleResourceList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<SysRoleResource>> userResourceList(@RequestParam("roleId") Long roleId) {
        List<SysRoleResource> sysUserResourceList = sysRoleResourceService.findRoleResourceByRoleId(roleId);
        return ReturnResult.success(sysUserResourceList);
    }

    @ApiOperation("保存角色资源")
    @RequestMapping(value = "/saveRoleResource" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveUserResource(@RequestParam("roleId") Long roleId , @RequestParam("resourceIds[]") List<Long> resourceIds ) {
        sysRoleResourceService.saveOrUpdateRoleResource(resourceIds ,  roleId ) ;
        return ReturnResult.success("OK");
    }

    @ApiOperation("角色列表")
    @RequestMapping(value = "/roleList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<SysRole>> roleList() {
        return ReturnResult.success(sysRoleService.findAll());
    }

    @ApiOperation("角色详情")
    @RequestMapping(value = "/roleDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<SysRole> roleDetail(@RequestParam(value = "roleId" , required = false) Long roleId) {
        return ReturnResult.success(sysRoleService.findById(roleId));
    }

    @ApiOperation("保存角色")
    @RequestMapping(value = "/saveRole" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveRole(@RequestParam(value = "roleId" , required = false) Long roleId , @RequestParam("roleName") String roleName ) {
        sysRoleService.saveOrUpdateRole(roleId , roleName);
        return ReturnResult.success("OK");
    }

    @ApiOperation("删除角色")
    @RequestMapping(value = "/delRole" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveRole(@RequestParam("roleId") Long roleId ) {
        sysRoleService.deleteRole(roleId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("角色用户列表")
    @RequestMapping(value = "/roleUserList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<SysUser>> roleUserList( @RequestParam("roleId") Long roleId ) {
        List<Long> userIds = sysUserRoleService.findSysUserByRoleId(roleId);
        if(userIds.isEmpty()){
            return ReturnResult.success(Collections.EMPTY_LIST);
        }else{
            return ReturnResult.success(sysUserService.findByIds(userIds));
        }
    }

    @ApiOperation("回收角色")
    @RequestMapping(value = "/recycleRole" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> recycleRole(@RequestParam("userId") Long  userId  , @RequestParam("roleId") Long roleId ) {
        sysUserRoleService.deleteUserRole( userId , roleId );
        return ReturnResult.success("OK");
    }


    @ApiOperation("保存更新用户角色")
    @RequestMapping(value = "/saveUserRole" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveUserRole(@RequestParam("roleIds[]") List<Long> roleIds,@RequestParam("userId") Long userId  ) {
        sysUserRoleService.saveOrUpdateUserRole(roleIds, userId );
        return ReturnResult.success("OK");
    }

    @ApiOperation("用户角色列表")
    @RequestMapping(value = "/userRoleList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<SysUserRole>> userRoleList(@RequestParam("userId") Long userId  ) {
        List<SysUserRole> userRoleList = sysUserRoleService.findRoleByUserId(userId);
        return ReturnResult.success(userRoleList);
    }


    @ApiOperation("业务组列表")
    @RequestMapping(value = "/busTeamList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<BusTeam>> busTeamList(@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<BusTeam> page = busTeamService.findBusTeamForPage(pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("业务组详情")
    @RequestMapping(value = "/busTeamDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<BusTeam> busTeamDetail(@RequestParam("busTeamId") Long busTeamId ) {
        BusTeam busTeam = busTeamService.findById(busTeamId);
        return ReturnResult.success(busTeam);
    }

    @ApiOperation("保存更新业务组")
    @RequestMapping(value = "/saveBusTeam" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveBusTeam(@RequestParam(value = "busTeamId",required = false) Long busTeamId ,@RequestParam(value = "areaTeamId" , required = false ) Long areaTeamId ,@RequestParam(value = "name",required = false ) String name) {
        busTeamService.saveOrUpdateBusTeam(busTeamId , areaTeamId ,name );
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除业务组")
    @RequestMapping(value = "/delBusTeam" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delBusTeam(@RequestParam(value = "busTeamId") Long busTeamId ) {
        busTeamService.deleteBusTeam(busTeamId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("区域组列表")
    @RequestMapping(value = "/areaTeamList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<AreaTeam>> areaTeamList(@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<AreaTeam> page = areaTeamService.findAreaTeamForPage(pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("区域组详情")
    @RequestMapping(value = "/areaTeamDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<AreaTeam> areaTeamDetail(@RequestParam("areaTeamId") Long areaTeamId ) {
        AreaTeam areaTeam = areaTeamService.findById(areaTeamId);
        return ReturnResult.success(areaTeam);
    }

    @ApiOperation("区域更新业务组")
    @RequestMapping(value = "/saveAreaTeam" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveAreaTeam(@RequestParam(value = "areaTeamId",required = false) Long areaTeamId , @RequestParam(value = "name",required = false ) String name , @RequestParam("province") String province , @RequestParam("city") String city) {
        areaTeamService.saveOrUpdateAreaTeam(areaTeamId  ,name,province,city );
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除区域组")
    @RequestMapping(value = "/delAreaTeam" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delAreaTeam(@RequestParam(value = "areaTeamId") Long areaTeamId ) {
        areaTeamService.deleteAreaTeam(areaTeamId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("期列表")
    @RequestMapping(value = "/periodList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Period>> periodList(@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<Period> page = periodService.findPeriodForPage(pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("期详情")
    @RequestMapping(value = "/periodDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<PeriodVo> periodDetail(@RequestParam("periodId") Long periodId) {
        PeriodVo periodVo = periodService.findPeriodByPeriodId(periodId);
        return ReturnResult.success(periodVo);
    }


    @ApiOperation("保存更新期")
    @RequestMapping(value = "/saveOrUpdatePeriod" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdatePeriod(PeriodSaveOrUpdateVo periodSaveOrUpdateVo ,@RequestParam("segmentList") String segmentsJSONStr, HttpServletRequest request ) {
        List<PeriodSegmentVo> segmentList = JSONObject.parseObject(segmentsJSONStr, new TypeReference<List<PeriodSegmentVo>>() {
        });
        periodService.saveOrUpdatePeriod(periodSaveOrUpdateVo , segmentList );
        return ReturnResult.success("Ok");
    }


}
